<?php
require('lib/fpdf17/fpdf.php');

class PDF extends FPDF {

	function Header() {
		$this->Image('images/pdf_logo.png',10,7,24);
		$this->SetFont('Arial','B',13);
		$this->Cell(191,7,'Felix Martin Hi-Fi und Videostudios e.U.',0,1,R);
		$this->SetFont('Arial','',11);
		$this->Cell(191,5,'Neubaugasse 7/1/3',0,1,R);
		$this->Cell(191,5,'A-1060 Wien',0,1,R);
		$this->Line(10,29,200,29);
   	}

 	function Footer() {
 		$this->Line(10,280,200,280);

 		$this->SetXY(9,-15);
 		$this->SetFont('Arial','',8);
		$this->Cell(1,3,'Bankverbindung: Bank Austria',0,2,L);
		$this->Cell(1,3,'BLZ: 12000',0,2,L);
		$this->Cell(1,3,'KontoNr.: 098722-451-154',0,2,L);

		$this->SetXY(90,-15);
 		$this->SetFont('Arial','',8);
		$this->Cell(1,3,'FirmenbuchNr.: FN177033p',0,2,L);
		$this->Cell(1,3,'UmsatzsteuerID: ATU45782301',0,2,L);
		$this->Cell(1,3,'Gerichtsstand: Wien',0,2,L);

 		$this->SetXY(200,-15);
 		$this->SetFont('Arial','',8);
		$this->Cell(1,3,'Tel.: +43 1 330 15',0,2,R);
		$this->Cell(1,3,'e-Mail: office@martin-hifi.at',0,2,R);
		$this->Cell(1,3,'www.martin-hifi.at',0,2,R);
 	}

 	function Kunde($row) {
 		$this->SetXY(20, 49);
 		$this->SetFont('Arial','',12);

		if ((isset($row['Firma'])) || !($row['Firma'] =="")) {
			$this->Cell(1,5,'Firma',0,2,L);
			$this->Cell(1,5,$row['Firma'],0,2,L);
		} else {
			$this->Cell(1,5,$row['Anrede'],0,2,L);
			$this->Cell(1,5,$row['Vorname'] . ' ' . $row['Nachname'],0,2,L);
		}
		$this->Cell(1,5,$row['Strasse'],0,2,L);
		$this->Cell(1,5,$row['PLZ'] . ' ' . $row['Ort'],0,2,L);
		if ((isset($row['Firma'])) || !($row['Firma'] =="")) {
			$this->Cell(1,5,$row['USTIDNR'],0,2,L);
		}
 	}

 	function Barverkauf() {
 		$this->SetXY(20, 49);
 		$this->SetFont('Arial','',12);
 		$this->Cell(1,5,'Barverkauf',0,2,L);
 	}

 	function Status($row) {
 		$this->SetXY(160, 49);
 		$this->SetFont('Arial','B',11);
 		$this->Cell(1,5,'Lieferschein Nr.:',0,2,R);
		$this->SetFont('Arial','',11);
 		$this->Cell(2,5,'Mitarbeiter Nr.: ',0,2,R);
 		$this->Cell(2,5,'Kunden Nr.: ',0,2,R);
 		$this->Cell(2,7,'Liefer-Datum: ',0,2,R);
 		
 		$this->SetXY(190, 49);
 		$this->SetFont('Arial','B',11);
 		$this->Cell(1,5,'L' . $row['RegID'],0,2,R);
 		$this->SetFont('Arial','',11);
 		$this->Cell(1,5,$row['MitarbeiterID'],0,2,R);
 		$this->Cell(1,5,$row['KundeID'],0,2,R);
 		$this->Cell(1,7,$row['Lieferdatum'],0,2,R);
 	}

 	function Betreff($row) {
 		$this->Line(20,108,190,108);
 		$this->Line(20,117,190,117);
 		$this->Line(20,239,190,239);
 		
 		$this->Line(20,108,20,239);
 		$this->Line(190,108,190,239);

 		$this->SetXY(20,98);
 		$this->SetFont('Arial','B',16);
 		$this->Cell(10,5,'Lieferschein',0,2,L);
 		$this->SetXY(25,110);
 		$this->SetFont('Arial','B',10);
 		$this->Cell(10,5,'Art-Nr. ',0,0,R);
 		$this->Cell(70,5,'Artikelname',0,0,L);
 		$this->Cell(21,5,'Menge',0,0,C);
 	}

 	 function RechnungInfo($row) {
 		$this->SetFont('Arial','B',10);
 		$this->SetXY(140,98);
 		$this->Cell(20,6,utf8_decode('Montage gewählt: '),0,0,L);
 		if ($row['Montage']!=0) {
 			$this->Cell(30,6,'ja',0,2,R);
 		} else {
 			$this->Cell(30,6,'nein',0,2,R);
 		}
 		$this->SetFont('Arial','',10);
 		$this->SetXY(57,98);
		$this->Cell(24,6,'zu Rg-Nr.: ' . $row['RegID'],0,2,R);
 	}

 	function Artikel($row, $artikel, $numArtikel) {
 		$this->SetXY(25,119);
 		$this->SetFont('Arial','',10);

 		for ($i=0; $i < $numArtikel; $i++) { 
 			$this->Cell(10,5,$artikel[$i]['ArtikelID'].'  ',0,0,R);
 			$this->Cell(75,5,$artikel[$i]['Artikelname'],0,0,L);
 			$this->Cell(10,5,$artikel[$i]['Menge'],0,1,C);
 			$this->SetLeftMargin(25);
 		}
 	}
}

//Start
ConnectMySql();

function ConnectMySql() {
	if (isset($_POST['erstellePDF'])) {
		
   		include('inc/db_connect.php');
		
		$query = mysql_query("SELECT *, Rechnung.ID RegID, Mitarbeiter.Vorname VN, Mitarbeiter.Nachname NN, Rechnungstorno.RechnungStornoID Storno, Rechnungstorno.RechnungOriginalID StornoRg FROM Rechnung
		LEFT JOIN Lieferung on Rechnung.Lieferung=Lieferung.ID
		LEFT JOIN Mitarbeiter on Rechnung.MitarbeiterID=Mitarbeiter.ID
		LEFT JOIN Kunde on Rechnung.KundeID=Kunde.ID
		LEFT JOIN Adresse on Kunde.Adresse=Adresse.ID
		LEFT JOIN Rechnungstorno on Rechnungstorno.RechnungStornoID=Rechnung.ID 
		WHERE Rechnung.ID=" . $_POST['erstellePDF']);
		$row = mysql_fetch_array($query);	    

		$queryArtikel = mysql_query("SELECT * FROM rechnungartikel reg
			LEFT JOIN artikel on reg.ArtikelID = artikel.id
			LEFT JOIN kategorie on artikel.kategorie = kategorie.id
			WHERE reg.RechnungID=" . $_POST['erstellePDF']);
		$numArtikel=mysql_num_rows ($queryArtikel);
 		
 		for ($i=0; $i < $numArtikel; $i++) { 
           	$artikel[$i] = mysql_fetch_array($queryArtikel);
        }

		createPDF($row, $artikel, $numArtikel);
 	
 	} else {
 		
 	}
}

function CreatePDF($row, $artikel, $numArtikel) {
	$pdf = new PDF('P','mm','A4');
	$pdf->AliasNbPages();
	$pdf->AddPage();
	$pdf->Kunde($row);
	$pdf->RechnungInfo($row);
	$pdf->Status($row);
	$pdf->Betreff($row);
	$pdf->Artikel($row, $artikel, $numArtikel);
	$pdf->Output('Lieferschein_L' . $row['RegID'] . '_' . $row['Lieferdatum'],I);
}
?>